var express = require('express');
var router = express.Router();
var xlsx = require('node-xlsx');
var sql = require('./../sql/index')
var User = require('./../sql/col/users')
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
/**
 * @api {post} /users/add 添加用户
 * @apiDescription 添加用户
 * @apiName users
 * @apiGroup users
 * @apiParam { string } tel 手机号
 * @apiParam { string } password 密码
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '加入成功'
    })
    res.send({
        code: '10101',
        message: '该用户已存在'
      })
 * @apiSampleRequest /users/add
 * @apiVersion 1.0.0
 */
router.post('/add', function(req, res, next) {
  // res.send('respond with a resource');
  const { tel, password } = req.body // post
  // get  req.query
  sql.find(User, { tel }, { _id: 0}).then(data => {
    if (data.length === 0) {
      sql.insert(User, {
        tel: tel,
        password: password
      }).then(() => {
        res.send({
          code: '200',
          message: '加入成功'
        })
      })
    } else {
      res.send({
        code: '10101',
        message: '该用户已存在'
      })
    }
  })

  
});
var fileurl = "E:/course-wxx/xingye/lesson4/myapp/1902.xlsx"
router.get('/upload', (req, res, next) => {
  res.send(xlsx.parse(fileurl)[0].data)
})
module.exports = router;
